package com.example.controller;

import com.example.entity.User;
import com.example.service.IConsumerService;
import com.example.service.IUserService;
import com.example.service.RocketService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author ：liukai
 * @date ：Created in 2021/9/6 11:00 上午
 * @description：
 * @modified By：
 * @version: $
 */
@RestController
@RefreshScope
@Slf4j
public class ConsumerController {

    @Autowired
    public RestTemplate restTemplate;

    @Resource
    private IUserService iUserService;
    @Resource
    private IConsumerService iConsumerService;
    @Resource
    private RocketService rocketService;

    @Value("${service-url.nacos-user-service}")
    public String serviceName;

    @Value("${config.info}")
    public String info;

    @Value("${share.info}")
    public String shareInfo;

    @GetMapping("/info")
    public String info() {
        return info;
    }

    @GetMapping("/shareInfo")
    public String share() {
        return shareInfo;
    }

    @GetMapping("/getById/{id}")
    public String getById(@PathVariable("id") int id) {

        String result = restTemplate.getForObject(serviceName + "/provider/getById/" + id, String.class);

        return "获取成功：" + id + "=============" + result;
    }

    @GetMapping("/getById2/{id}")
    public String getById2(@PathVariable("id") int id) {

        String result = iUserService.getById(id);

        return "获取成功：" + id + "=============" + result;
    }

    @GetMapping("/addUser/{id}")
    public String addUser(@PathVariable("id") int id) {
        User user = User.builder()
                .username("user1")
                .password("password1")
                .build();
        String result = iUserService.addUser(user);
        return "id为" + id + "的用户添加成功" + "==============" + result;
    }

    @GetMapping("consumer/circuit/getOK")
    public String getUserOK() {
        return iConsumerService.getUserOK(100);
    }

    @GetMapping("/consumer/circuit/getOneTimeOut")
    public String getUserTimeOut1() {
        return iConsumerService.getUserTimeOut(100);
    }

    @GetMapping("/consumer/circuit/getManyTimeOut/{count}")
    public String getUserTimeOut(@PathVariable("count") int count) {

        for (int i = 0; i < count; i++) {
            new Thread(() -> {
                String result = iConsumerService.getUserTimeOut(Integer.valueOf(Thread.currentThread().getName()));
                log.info(Thread.currentThread().getName() + ":getUserTimeOut==========" + result);
            }, i + "").start();
        }

        return "请求完毕";
    }

    @GetMapping("/consumer/sendUser")
    public String send() {
        return rocketService.send(User.builder()
                .username("liukai")
                .password("123456")
                .build());
    }

}
